package com.runen.infinitemovement.widget.popupwind;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.tool.CommonUtil;

import static com.runen.infinitemovement.tool.CommonUtil.getColor;


/**
 * Created by LiaoDuanHong  on 2018/10/8
 */

public class CommonDialog implements View.OnClickListener {
    private static CommonDialog commonDialog = null;
    private AlertDialog dialog;
    private final TextView tvPhone;
    private final Button btn_cancel;
    private final Button btn_sure;
    private final TextView v_common_dialog_lien;
    private final AlertDialog.Builder builder;
    private OnClickListener onClickListener;
    private TextView tv_title;


    public OnClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setCancelable() {
        if (dialog != null) {
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
        }
    }

    public static CommonDialog getInstance(Context context, String content) {
        return new CommonDialog(context, content);
    }

    public static CommonDialog getInstance(Context context, String content, String sure) {
        return new CommonDialog(context, content, sure);
    }


    public static CommonDialog getInstance(Context context, String content, String sure, int color) {
        return new CommonDialog(context, content, sure, color);
    }

    public static CommonDialog getInstance(Context context, String title, String content, String sure) {
        return new CommonDialog(context, title, content, sure);
    }

    public static CommonDialog getInstance(Context context, String title, String content, String sure, String cancel, int type) {
        return new CommonDialog(context, title, content, sure, cancel, type);
    }

    private CommonDialog(Context context, String content, String sure, int color) {
        builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.item_play_phone, null);
        builder.setView(view);
        tv_title = view.findViewById(R.id.tv_title);
        tv_title.setVisibility(View.GONE);
        tvPhone = (TextView) view.findViewById(R.id.tv_phone);
        btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        btn_sure = (Button) view.findViewById(R.id.btn_sure);
        v_common_dialog_lien = (TextView) view.findViewById(R.id.v_common_dialog_lien);
        btn_sure.setText(sure);
        btn_sure.setTextColor(getColor(color));
        tvPhone.setText(content);
        dialog = builder.show();
        initEvent();
    }

    private CommonDialog(Context context, String content, String sure) {
        builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.item_play_phone, null);
        builder.setView(view);
        tv_title = view.findViewById(R.id.tv_title);
        tv_title.setVisibility(View.GONE);
        tvPhone = (TextView) view.findViewById(R.id.tv_phone);
        btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        btn_sure = (Button) view.findViewById(R.id.btn_sure);
        v_common_dialog_lien = (TextView) view.findViewById(R.id.v_common_dialog_lien);
        btn_sure.setText(sure);
        tvPhone.setText(content);
        dialog = builder.show();
        initEvent();
    }

    private CommonDialog(Context context, String content) {
        builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.item_play_phone, null);
        builder.setView(view);
        tvPhone = (TextView) view.findViewById(R.id.tv_phone);
        btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        btn_sure = (Button) view.findViewById(R.id.btn_sure);
        tv_title = view.findViewById(R.id.tv_title);
        tv_title.setVisibility(View.GONE);
        btn_sure.setText("确定");
        btn_sure.setTextColor(getColor(R.color.color_FFF63E5A));
        v_common_dialog_lien = (TextView) view.findViewById(R.id.v_common_dialog_lien);
        v_common_dialog_lien.setVisibility(View.GONE);
        btn_cancel.setVisibility(View.GONE);
        tvPhone.setText(content);
        dialog = builder.show();
        initEvent();
    }

    private CommonDialog(Context context, String title, String content, String sure) {
        builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.item_play_phone, null);
        builder.setView(view);
        tv_title = view.findViewById(R.id.tv_title);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText(title);
        tvPhone = (TextView) view.findViewById(R.id.tv_phone);
        btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        btn_sure = (Button) view.findViewById(R.id.btn_sure);
        v_common_dialog_lien = (TextView) view.findViewById(R.id.v_common_dialog_lien);
        btn_sure.setText(sure);
        tvPhone.setText(content);
        dialog = builder.show();
        initEvent();
    }

    private CommonDialog(Context context, String title, String content, String sure, String cancel, int type) {
        builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.item_play_phone, null);
        builder.setView(view);
        tv_title = view.findViewById(R.id.tv_title);
        if (type == 1) {
            tv_title.setVisibility(View.VISIBLE);
        } else {
            tv_title.setVisibility(View.GONE);
        }
        tv_title.setText(title);
        tvPhone = (TextView) view.findViewById(R.id.tv_phone);
        btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        btn_sure = (Button) view.findViewById(R.id.btn_sure);
        v_common_dialog_lien = (TextView) view.findViewById(R.id.v_common_dialog_lien);
        btn_cancel.setText(cancel);
        btn_sure.setText(sure);
        tvPhone.setText(content);
        dialog = builder.show();
        initEvent();
    }

    private void initEvent() {
        //关闭Dialog
        btn_cancel.setOnClickListener(this);
        btn_sure.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_cancel:
                if (onClickListener != null) {
                    onClickListener.onDismiss();
                }
                dialog.dismiss();
                break;
            case R.id.btn_sure:
                if (onClickListener != null) {
                    onClickListener.onSendListener();
                }
                dialog.dismiss();
                break;
            default:
                break;

        }
    }

    public interface OnClickListener {
        void onSendListener();

        void onDismiss();
    }
}
